package org.csu.gp_store.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.csu.gp_store.alipay.AlipayBean;
import org.csu.gp_store.common.CommonResponse;

import org.csu.gp_store.entity.OrderItem;
import org.csu.gp_store.entity.Orders;
import org.csu.gp_store.service.OrderItemService;
import org.csu.gp_store.service.OrderService;
import org.csu.gp_store.util.AlipayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CSU_Luoyuhang
 */
@Controller
@RequestMapping(value = "/orders")
public class AliPayController {
    @Autowired
    AlipayUtil alipayUtil;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @PostMapping(value = "/alipay/{orderId}")
    @ResponseBody
    //String outTradeNo, String subject, String totalAmount, String body
    public CommonResponse<String> alipay(
            AlipayBean alipayBean,
            @PathVariable("orderId") String orderId,
            @RequestParam(name = "subject") String subject,
            @RequestParam(name = "totalAmount") String totalAmount) throws AlipayApiException {
        System.out.println("********************************************************************************");
        System.out.println(orderId);
        return CommonResponse.createForSuccess(alipayUtil.pay(alipayBean, orderId, subject, totalAmount));

    }

    @GetMapping(value = "/return")
    @ResponseBody
    //String outTradeNo, String subject, String totalAmount, String body
    public String returnUrl(HttpServletRequest request) throws AlipayApiException {

        //需要一个返回的网址
        return null;
    }

    @PostMapping( "/notify")
    @ResponseBody
    public String notifyUrl(HttpServletRequest request) throws Exception{
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            System.out.println("=========支付宝异步回调========");

            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
                 System.out.println(name + " = " + request.getParameter(name));
            }

            String tradeNo = params.get("out_trade_no");
            System.out.println(tradeNo);
            System.out.println("*************************************111111111111");
            orderService.updateOrderStatus(Long.valueOf(tradeNo),1);
            List<OrderItem> orderItemList=orderItemService.getOrderItemsByOrderID(Long.valueOf(tradeNo));
            for (OrderItem orderItem : orderItemList){
                orderItemService.updateOrderItemStatus(orderItem.getIndexID(), 1);
            }
            System.out.println("*************************************111111111111");

        }
        return "success";

//       try {
//           String out_trade_no = request.getParameter("out_trade_no");
//           AlipayTradeQueryResponse response = alipayUtil.tradeCheck(out_trade_no);
//           String trade_status = response.getTradeStatus();
//           Orders orders=orderService.getOrderByOrderID(Long.valueOf(out_trade_no));
//           orders.setStatus(1111);
//           if (trade_status.equals("TRADE_SUCCESS")) {
//               orderService.updateOrder(orders.getOrderID(), orders);
//           }
//       }catch (Exception e){
//           e.printStackTrace();
//       }
//        return "Success";
    }
}
